package org.edu.modules.oa.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.model.PutObjectResult;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import groovy.util.logging.Slf4j;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.shiro.SecurityUtils;
import org.edu.common.api.vo.Result;
import org.edu.common.system.vo.LoginUser;
import org.edu.modules.oa.entity.VeFileFilesEntity;
import org.edu.modules.oa.entity.VeOaMeetSummaryEntity;
import org.edu.modules.oa.mapper.IVeFileFilesMapper;
import org.edu.modules.oa.service.IVeOaMeetSummaryService;
import org.edu.modules.oa.util.OssDownloadUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.UUID;

@Api(tags = "会议管理---纪要")
@RestController
@RequestMapping("oa/veOaMeetSummary")
@Slf4j
@ApiSort(value = 100)
public class VeOaMeetSummaryController {

    @Resource
    private IVeOaMeetSummaryService veOaMeetSummaryService;
    @Resource
    private OSS ossClient;
    @PostMapping("/loadSummary")
    @ApiOperation(value="显示表格", notes="纪要管理--显示表格")
    public Result<?> loadTable(Integer start,Integer pageSize,String title, Integer meetId){
        HashMap<String,Object> map = veOaMeetSummaryService.loadTable(start,pageSize,title,meetId);
        return Result.OK(map);
    }

    @PostMapping("/deleteSummaryOne")
    @ApiOperation(value="纪要管理", notes="纪要管理--删除一条纪要")
    public Result<?> deleteSummaryOne(Integer id){
        int res = veOaMeetSummaryService.deleteSummaryById(id);
        if(res == 0){
            return Result.error(500,"删除失败了");
        }
        return Result.OK();
    }

    @PostMapping("/deleteSummaryAll")
    @ApiOperation(value="纪要管理", notes="纪要管理--删除一条纪要")
    public Result<?> deleteSummaryAll(String ids){
        String [] arr = ids.split(",");
        for (String s : arr) {
            if(s != null && !s.equals("")){
                veOaMeetSummaryService.deleteSummaryById(Integer.parseInt(s));
            }
        }
        return Result.OK();
    }

    @PostMapping("/updateSummary")
    @ApiOperation(value="纪要管理", notes="纪要管理--修改纪要")
    public Result<?> updateSummary(MultipartFile file, VeOaMeetSummaryEntity veOaMeetSummaryEntity)throws Exception {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        veOaMeetSummaryEntity.setUpdateName(user.getRealname());
        veOaMeetSummaryEntity.setUpdateBy(user.getId());
        //创建文件类
        VeFileFilesEntity veFileFilesEntity = null;
        String fileName = null;
        String suffixName = null;
        if(file != null){
            //myFile为MultipartFile类型
//            CommonsMultipartFile cf= (CommonsMultipartFile)file;
//            DiskFileItem fi = (DiskFileItem)cf.getFileItem();
//            File document = fi.getStoreLocation();
            //把MultipartFile文件流转换成文件
            File document = null;
            //获取文件名
            fileName = file.getOriginalFilename();
            //用点来切割获取文件类型注意//.需要加双斜杠正则表达式
            String[] arr = fileName.split("\\.");
            //获取文件后缀名
            suffixName = arr[arr.length-1];
            if(suffixName.equals("java") || suffixName.equals("class")){
                return Result.error(400,"文件类型不能上传");
            }
            try {
                String originalFilename = file.getOriginalFilename();
                String[] filename = originalFilename.split("\\.");
                document=File.createTempFile(filename[0], filename[1]);
                file.transferTo(document);
                document.deleteOnExit();
            } catch (IOException e) {
                e.printStackTrace();
            }
            //获取文件长度
            Long size = file.getSize();
            //获取唯一的文件名称
            String docName = UUID.randomUUID().toString();
            //上传文件到指定的存储空间  第一个参数 bucketName  第二个参数 OSS存储路径  第三个参数 要上传的文件
//            PutObjectResult result = ossClient.putObject("digital-school-xx0422","school-demo/"+fileName,document);
            PutObjectResult result = ossClient.putObject("exaplebucket-beijing", docName+"/"+fileName, document);
            //文件下载地址
            String path = "https://digital-school-xx0422.oss-cn-shenzhen.aliyuncs.com/school-demo/"+fileName;
            veFileFilesEntity = new VeFileFilesEntity();
            //当前插入的时间
            Long time = System.currentTimeMillis()/1000;
            veFileFilesEntity.setSourceFile("https://exaplebucket-beijing.oss-cn-beijing.aliyuncs.com/"+docName+"/"+fileName);
            veFileFilesEntity.setName(fileName);
            veFileFilesEntity.setExt(suffixName);
            veFileFilesEntity.setCreateTime(time);
            veFileFilesEntity.setSize(size);
            System.out.println(veOaMeetSummaryEntity);
            System.out.println(veFileFilesEntity);
        }
        int res = veOaMeetSummaryService.updateSummary(veOaMeetSummaryEntity,veFileFilesEntity);
        if(res == 0 ){
            return Result.error(500,"修改失败");
        }
        return Result.OK();
    }

    @PostMapping("/insertSummary")
    @ApiOperation(value="纪要管理", notes="纪要管理--添加纪要")
    public Result<?> insertSummary(MultipartFile file, VeOaMeetSummaryEntity veOaMeetSummaryEntity)throws Exception {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        veOaMeetSummaryEntity.setCreateName(user.getRealname());
        veOaMeetSummaryEntity.setCreateBy(user.getId());
        //创建文件类
        VeFileFilesEntity veFileFilesEntity = null;
        String fileName = null;
        String suffixName = null;
        if(file != null){
            //获取文件名
            fileName = file.getOriginalFilename();
            //用点来切割获取文件类型
            String[] arr = fileName.split("\\.");
            //获取文件后缀名
            suffixName = arr[arr.length-1];
            if(suffixName.equals("java") || suffixName.equals("class")){
                return Result.error(400,"改类型文件不允许上传");
            }
            //获取文件长度
            Long size = file.getSize();
            //把MultipartFile文件流转换成文件
            File document = null;
            try {
                String originalFilename = file.getOriginalFilename();
                String[] filename = originalFilename.split("\\.");
                document=File.createTempFile(filename[0], filename[1]);
                file.transferTo(document);
                document.deleteOnExit();
            } catch (IOException e) {
                e.printStackTrace();
            }
            //获取唯一的文件名称
            String docName = UUID.randomUUID().toString();
            //上传文件到指定的存储空间  第一个参数 bucketName  第二个参数 OSS存储路径  第三个参数 要上传的文件
//            PutObjectResult result = ossClient.putObject("digital-school-xx0422","school-demo/"+docName,document);
            PutObjectResult result = ossClient.putObject("exaplebucket-beijing",docName + "/"+ fileName, document);
            String path = "https://digital-school-xx0422.oss-cn-shenzhen.aliyuncs.com/school-demo/"+docName;
            veFileFilesEntity = new VeFileFilesEntity();
            //当前插入的时间
            Long time = System.currentTimeMillis()/1000;
            veFileFilesEntity.setSourceFile("https://exaplebucket-beijing.oss-cn-beijing.aliyuncs.com/"+docName+"/"+fileName);
            veFileFilesEntity.setName(fileName);
            veFileFilesEntity.setExt(suffixName);
            veFileFilesEntity.setCreateTime(time);
            veFileFilesEntity.setSize(size);
        }
        System.out.println(veOaMeetSummaryEntity);
        System.out.println(veFileFilesEntity);
        int res = veOaMeetSummaryService.insertSummary(veOaMeetSummaryEntity,veFileFilesEntity);
        if(res == 0 ){
            return Result.error(500,"添加失败");
        }
        return Result.OK();
    }
}
